package com.example.util;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 邮件发送工具包
 *
 * @author pursueSong
 * 2022-04-12
 */
public class EmailUtil {

    /**
     * @param cc 发送人
     * @param text 内容主体
     * @param title 标题
     * @param to 接受人呢
     * @throws MessagingException
     */
    public void send(String cc,String title,String text, String to ) throws MessagingException {
        Properties properties = new Properties();
        properties.setProperty("mail.debug","true");
        properties.setProperty("mail.smtp.auth","true");
        properties.setProperty("mail.smtp.protocol","smtps");
        Session session = Session.getInstance(properties);
        Message message = new MimeMessage(session);
        message.setText(text);
        message.setFrom(new InternetAddress(cc));
        message.setSubject(title);
        Transport transport = session.getTransport();
        transport.connect("smtp.qq.com",cc,"luriymtrgvkqdbef");
        transport.sendMessage(message,new Address[]{new InternetAddress(to)});

        transport.close();
    }
}
